import 'package:flutter/material.dart';
// import 'package:font_awesome_flutter/font_awesome_flutter.dart'; // ئىشلىتىلمىگەنلىكى ئۈچۈن ئۆچۈرۈلدى

// import 'package:flutter/material.dart'; // قايتا كەلگەن ئىمپورتنى ئۆچۈرۈش
import 'package:fl_chart/fl_chart.dart';
// تۈر نامىنى 'tijarat' دىن 'inventory_system' گە ئۆزگەرتىش
import 'package:inventory_system/screens/profile_screen.dart'; 
import 'package:inventory_system/screens/settings_screen.dart';
import 'package:inventory_system/screens/login_screen.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  // تىزىملىكنىڭ ھەركەتچان ھالىتى ئۈچۈن مەلۇماتلار
  bool _isMenuExpanded = true; // تىزىملىك كەڭەيگەن ياكى كىچىكلىگەن ھالەتتە
  final double _expandedWidth = 256; // كەڭەيگەن ھالەتتىكى كەڭلىك
  final double _collapsedWidth = 90; // كىچىكلىگەن ھالەتتىكى كەڭلىك
  
  // باسقان تىزىملىك تۈرىنىڭ نامى
  String _activeMenuItem = 'باش بەت';
  bool _showNewProductBadge = true;
  bool _showNewSaleBadge = true;
  bool _showNewReportBadge = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Row( // ئىككى قىسىملىق قىلىش
        children: [
          // ئاساسلىق مەزمۇن
          Expanded(
            child: LayoutBuilder(
              builder: (context, constraints) {
                return SizedBox(
                  width: constraints.maxWidth,
                  child: Column(
                    children: [
                      _buildTopBar(),
                      Expanded(
                        child: SingleChildScrollView(
                          child: Column(
                            children: [
                              _buildStatisticsCards(),
                              _buildQuickAccessButtons(),
                              _buildCharts(),
                              _buildRecentActivities(),
                              _buildStockReports(),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
          // يول باشلاش تىزىملىكى (يېڭىلانغان ھەركەتچان تىزىملىك)
          _buildSidebar(),
        ],
      ),
    );
  }

  // يۇقىرى قىسىم
  Widget _buildTopBar() {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // ئىزدەش قىسمى
          Expanded(
            child: Container(
              margin: const EdgeInsets.only(left: 16),
              decoration: BoxDecoration(
                color: Colors.grey[100],
                borderRadius: BorderRadius.circular(4),
              ),
              child: TextField(
                textDirection: TextDirection.rtl,
                decoration: InputDecoration(
                  hintText: 'ئىزدەش...',
                  hintTextDirection: TextDirection.rtl,
                  prefixIcon: const Icon(Icons.search, color: Colors.grey),
                  border: InputBorder.none,
                  contentPadding:
                      const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
                ),
              ),
            ),
          ),

          // كىچىك كۇنۇپكىلار
          Row(
            children: [
              PopupMenuButton<String>(
                icon: Icon(Icons.language, color: Colors.grey[600]),
                offset: const Offset(0, 40),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                itemBuilder: (BuildContext context) => [
                  _buildLanguageMenuItem(
                    flag: '🇨🇳',
                    language: 'ئۇيغۇرچە',
                    isSelected: true,
                  ),
                  _buildLanguageMenuItem(
                    flag: '🇨🇳',
                    language: '中文',
                    isSelected: false,
                  ),
                  _buildLanguageMenuItem(
                    flag: '🇺🇸',
                    language: 'English',
                    isSelected: false,
                  ),
                ],
                onSelected: (String value) {
                  // بۇ يەردە تىل ئۆزگەرتىش قىلىنىدۇ
                },
              ),
              const SizedBox(width: 8),
              PopupMenuButton<String>(
                icon: Icon(Icons.brush, color: Colors.grey[600]),
                offset: const Offset(0, 40),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                itemBuilder: (BuildContext context) => [
                  _buildThemeMenuItem(
                    icon: Icons.light_mode,
                    title: 'يورۇق ئۇسلۇب',
                    isSelected: true,
                  ),
                  _buildThemeMenuItem(
                    icon: Icons.dark_mode,
                    title: 'قارا ئۇسلۇب',
                    isSelected: false,
                  ),
                  _buildThemeMenuItem(
                    icon: Icons.settings_system_daydream,
                    title: 'سىستېما ئۇسلۇبى',
                    isSelected: false,
                  ),
                ],
                onSelected: (String value) {
                  // بۇ يەردە ئۇسلۇب ئۆزگەرتىش قىلىنىدۇ
                },
              ),
              const SizedBox(width: 8),
              Stack(
                children: [
                  IconButton(
                    icon: Icon(Icons.notifications, color: Colors.grey[600]),
                    onPressed: () {
                      _showNotificationsDialog();
                    },
                  ),
                  Positioned(
                    right: 8,
                    top: 8,
                    child: Container(
                      padding: const EdgeInsets.all(4),
                      decoration: const BoxDecoration(
                        color: Colors.red,
                        shape: BoxShape.circle,
                      ),
                      child: const Text(
                        '3',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 10,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(width: 8),
              PopupMenuButton<String>(
                icon: Icon(Icons.person, color: Colors.grey[600]),
                offset: const Offset(0, 40),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                ),
                itemBuilder: (BuildContext context) => [
                  _buildUserMenuItem(
                    icon: Icons.account_circle,
                    title: 'شەخسىي ئۇچۇر',
                  ),
                  _buildUserMenuItem(
                    icon: Icons.settings,
                    title: 'تەڭشەكلەر',
                  ),
                  const PopupMenuDivider(),
                  _buildUserMenuItem(
                    icon: Icons.logout,
                    title: 'چېكىنىش',
                  ),
                ],
                onSelected: (String value) {
                  switch (value) {
                    case 'شەخسىي ئۇچۇر':
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => const ProfileScreen(),
                        ),
                      );
                      break;
                    case 'تەڭشەكلەر':
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => const SettingsScreen(),
                        ),
                      );
                      break;
                    case 'چېكىنىش':
                      showDialog(
                        context: context,
                        builder: (context) => AlertDialog(
                          title: const Text('چېكىنىش'),
                          content: const Text('راستىنلا چېكىنمەكچىمۇ؟'),
                          actions: [
                            TextButton(
                              onPressed: () => Navigator.pop(context),
                              child: const Text('ياق'),
                            ),
                            TextButton(
                              onPressed: () {
                                Navigator.pop(context);
                                Navigator.pushReplacement(
                                  context,
                                  MaterialPageRoute(
                                    builder: (context) => const LoginScreen(),
                                  ),
                                );
                              },
                              child: const Text('ھەئە'),
                            ),
                          ],
                        ),
                      );
                      break;
                  }
                },
              ),
            ],
          ),
        ],
      ),
    );
  }

  // تىزىملىك ئاستىدىكى ئىشلەتكۈچى مەلۇماتلىرى
  PopupMenuItem<String> _buildUserMenuItem({
    required IconData icon,
    required String title,
  }) {
    return PopupMenuItem<String>(
      value: title,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(icon, color: Colors.grey[600], size: 20),
          const SizedBox(width: 12),
          Flexible(
            child: Text(
              title,
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ],
      ),
    );
  }

  // تىزىملىك ئاستىدىكى ئىشلەتكۈچى مەلۇماتلىرى
  Widget _buildSidebar() {
    return AnimatedContainer(
      duration: const Duration(milliseconds: 300),
      curve: Curves.easeInOutQuart,
      width: _isMenuExpanded ? _expandedWidth : _collapsedWidth,
      decoration: BoxDecoration(
        color: const Color(0xFF223152), // سەل ئۇچۇقراق كۆك رەڭ
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.2),
            blurRadius: 10,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        children: [
          // باش تېما
          Container(
            height: 70,
            padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 16),
            decoration: const BoxDecoration(
              color: Color(0xFF2C3E67), // سەل ئۇچۇقراق رەڭ
            ),
            child: Material(
              color: Colors.transparent,
              child: InkWell(
                onTap: () {
                  setState(() {
                    _isMenuExpanded = !_isMenuExpanded;
                  });
                },
                borderRadius: BorderRadius.circular(8),
                child: Center(
                  child: _isMenuExpanded
                    ? Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          const Expanded(
                            child: Text(
                              'تىجارەت ياردەمچىسى',
                              textDirection: TextDirection.rtl,
                              textAlign: TextAlign.right,
                              style: TextStyle(
                                color: Colors.white,
                                fontSize: 18,
                                fontWeight: FontWeight.bold,
                              ),
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        ],
                      )
                    : const Center(
                        child: Icon(
                          Icons.shopping_cart,
                          color: Colors.white,
                          size: 28,
                        ),
                      ),
                ),
              ),
            ),
          ),
          const Divider(color: Colors.white24, height: 1),
          // تىزىملىك تۈرلىرى
          Expanded(
            child: ListView(
              padding: const EdgeInsets.symmetric(vertical: 16),
              children: [
                _buildMenuItem(
                  icon: Icons.add_shopping_cart,
                  title: 'مال كىرگۈزۈش',
                  onTap: () {
                    setState(() {
                      _activeMenuItem = 'مال كىرگۈزۈش';
                      _showNewProductBadge = false;
                    });
                  },
                  isSelected: _activeMenuItem == 'مال كىرگۈزۈش',
                  showBadge: _showNewProductBadge,
                ),
                _buildMenuItem(
                  icon: Icons.shopping_cart_checkout,
                  title: 'مال سېتىش',
                  onTap: () {
                    setState(() {
                      _activeMenuItem = 'مال سېتىش';
                      _showNewSaleBadge = false;
                    });
                  },
                  isSelected: _activeMenuItem == 'مال سېتىش',
                  showBadge: _showNewSaleBadge,
                ),
                _buildMenuItem(
                  icon: Icons.inventory_2,
                  title: 'قامباردىكى مال',
                  onTap: () {
                    setState(() {
                      _activeMenuItem = 'قامباردىكى مال';
                    });
                  },
                  isSelected: _activeMenuItem == 'قامباردىكى مال',
                ),
                _buildMenuItem(
                  icon: Icons.account_balance_wallet,
                  title: 'مالىيە باشقۇرۇش',
                  onTap: () {
                    setState(() {
                      _activeMenuItem = 'مالىيە باشقۇرۇش';
                    });
                  },
                  isSelected: _activeMenuItem == 'مالىيە باشقۇرۇش',
                ),
                _buildMenuItem(
                  icon: Icons.bar_chart,
                  title: 'دوكلات ۋە ستاتىستىكا',
                  onTap: () {
                    setState(() {
                      _activeMenuItem = 'دوكلات ۋە ستاتىستىكا';
                      _showNewReportBadge = false;
                    });
                  },
                  isSelected: _activeMenuItem == 'دوكلات ۋە ستاتىستىكا',
                  showBadge: _showNewReportBadge,
                ),
                const Divider(color: Colors.white24, height: 32),
                _buildMenuItem(
                  icon: Icons.settings,
                  title: 'تەڭشەكلەر',
                  onTap: () {
                    setState(() {
                      _activeMenuItem = 'تەڭشەكلەر';
                    });
                  },
                  isSelected: _activeMenuItem == 'تەڭشەكلەر',
                ),
                _buildMenuItem(
                  icon: Icons.help_outline,
                  title: 'ياردەم',
                  onTap: () {
                    setState(() {
                      _activeMenuItem = 'ياردەم';
                    });
                  },
                  isSelected: _activeMenuItem == 'ياردەم',
                ),
                _buildMenuItem(
                  icon: Icons.info_outline,
                  title: 'دىتال ھەققىدە',
                  onTap: () {
                    setState(() {
                      _activeMenuItem = 'دىتال ھەققىدە';
                    });
                  },
                  isSelected: _activeMenuItem == 'دىتال ھەققىدە',
                ),
              ],
            ),
          ),
          // ئىشلەتكۈچى ئۇچۇرى
          AnimatedContainer(
            duration: const Duration(milliseconds: 300),
            curve: Curves.easeInOutQuart,
            padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 8),
            decoration: const BoxDecoration(
              color: Color(0xFF2C3E67), // سەل ئۇچۇقراق رەڭ
              border: Border(
                top: BorderSide(color: Colors.white24, width: 1),
              ),
            ),
            child: _isMenuExpanded ? 
              // كەڭەيگەن ھالەتتىكى قۇرۇلما
              AnimatedOpacity(
                opacity: _isMenuExpanded ? 1.0 : 0.0,
                duration: const Duration(milliseconds: 200),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        boxShadow: [
                          BoxShadow(
                            color: Colors.black.withOpacity(0.2),
                            blurRadius: 4,
                            offset: const Offset(0, 2),
                          ),
                        ],
                      ),
                      child: const CircleAvatar(
                        radius: 24,
                        backgroundColor: Color(0xFF5A6D9E),
                        child: Icon(Icons.person, color: Colors.white, size: 28),
                      ),
                    ),
                    const SizedBox(height: 12),
                    const Text(
                      'قەمبەر تۇرسۇن',
                      textDirection: TextDirection.rtl,
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 14,
                      ),
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 4),
                    Container(
                      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
                      decoration: BoxDecoration(
                        color: const Color(0xFF5A6D9E),
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: Text(
                        'باشقۇرغۇچى',
                        textDirection: TextDirection.rtl,
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: Colors.white.withOpacity(0.9),
                          fontSize: 10,
                          fontWeight: FontWeight.w500,
                        ),
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    const SizedBox(height: 12),
                    // چېكىنىش كۇنۇپكىسى
                    Material(
                      color: Colors.transparent,
                      child: InkWell(
                        onTap: () {
                          Navigator.pushReplacement(
                            context,
                            MaterialPageRoute(
                              builder: (context) => const LoginScreen(),
                            ),
                          );
                        },
                        hoverColor: const Color(0xFF3F5491).withOpacity(0.2),
                        splashColor: const Color(0xFF3F5491).withOpacity(0.3),
                        borderRadius: BorderRadius.circular(20),
                        child: Tooltip(
                          message: 'چېكىنىش',
                          child: Container(
                            padding: const EdgeInsets.all(8),
                            decoration: BoxDecoration(
                              shape: BoxShape.circle,
                              border: Border.all(
                                color: Colors.white30,
                                width: 1,
                              ),
                            ),
                            child: const Icon(
                              Icons.logout,
                              color: Colors.white70,
                              size: 16,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              )
              : 
              // كىچىكلىگەن ھالەتتىكى قۇرۇلما
              Container(
                alignment: Alignment.center,
                child: Container(
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black.withOpacity(0.2),
                        blurRadius: 4,
                        offset: const Offset(0, 2),
                      ),
                    ],
                  ),
                  child: const CircleAvatar(
                    radius: 24,
                    backgroundColor: Color(0xFF5A6D9E),
                    child: Icon(Icons.person, color: Colors.white, size: 28),
                  ),
                ),
              ),
          ),
        ],
      ),
    );
  }

  Widget _buildMenuItem({
    required IconData icon,
    required String title,
    required VoidCallback onTap,
    bool isSelected = false,
    bool showBadge = false,
  }) {
    return MouseRegion(
      cursor: SystemMouseCursors.click,
      child: AnimatedContainer(
        duration: const Duration(milliseconds: 200),
        margin: const EdgeInsets.symmetric(vertical: 4),
        decoration: BoxDecoration(
          color: isSelected ? const Color(0xFF3F5491).withOpacity(0.2) : Colors.transparent,
          borderRadius: BorderRadius.circular(8),
          border: Border.all(
            color: isSelected ? const Color(0xFF4F6BB5) : Colors.transparent,
            width: 1,
          ),
          boxShadow: isSelected ? [
            BoxShadow(
              color: const Color(0xFF3F5491).withOpacity(0.2),
              blurRadius: 4,
              offset: const Offset(0, 2),
            ),
          ] : null,
        ),
        child: Tooltip(
          message: _isMenuExpanded ? '' : title,
          preferBelow: false,
          child: InkWell(
            onTap: onTap,
            borderRadius: BorderRadius.circular(8),
            hoverColor: const Color(0xFF3F5491).withOpacity(0.1),
            splashColor: const Color(0xFF3F5491).withOpacity(0.2),
            highlightColor: const Color(0xFF3F5491).withOpacity(0.1),
            child: Container(
              padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
              child: _isMenuExpanded 
                ? // كەڭەيگەن ھالەتتە ئايكون ۋە تېكىست كۆرۈنىدۇ
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      AnimatedContainer(
                        duration: const Duration(milliseconds: 200),
                        padding: const EdgeInsets.all(6),
                        decoration: BoxDecoration(
                          color: isSelected ? const Color(0xFF3F5491).withOpacity(0.2) : Colors.transparent,
                          borderRadius: BorderRadius.circular(6),
                        ),
                        child: Icon(
                          icon,
                          color: isSelected ? Colors.white : Colors.white70,
                          size: 20,
                        ),
                      ),
                      const SizedBox(width: 8),
                      Expanded(
                        child: Text(
                          title,
                          style: TextStyle(
                            color: isSelected ? Colors.white : Colors.white70,
                            fontSize: 14,
                            fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
                          ),
                          textAlign: TextAlign.right,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      if (showBadge)
                        Container(
                          margin: const EdgeInsets.only(right: 4),
                          padding: const EdgeInsets.all(2),
                          decoration: const BoxDecoration(
                            color: Colors.red,
                            shape: BoxShape.circle,
                          ),
                          child: const Text(
                            '!',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 8,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                    ],
                  )
                : // كىچىكلىگەن ھالەتتە پەقەت ئايكونلا كۆرۈنىدۇ
                  Stack(
                    alignment: Alignment.center,
                    children: [
                      AnimatedContainer(
                        duration: const Duration(milliseconds: 200),
                        padding: const EdgeInsets.all(8),
                        decoration: BoxDecoration(
                          color: isSelected ? const Color(0xFF3F5491).withOpacity(0.2) : Colors.transparent,
                          borderRadius: BorderRadius.circular(6),
                        ),
                        child: Icon(
                          icon,
                          color: isSelected ? Colors.white : Colors.white70,
                          size: 24,
                        ),
                      ),
                      if (showBadge)
                        Positioned(
                          top: 0,
                          right: 0,
                          child: Container(
                            width: 10,
                            height: 10,
                            decoration: const BoxDecoration(
                              color: Colors.red,
                              shape: BoxShape.circle,
                            ),
                          ),
                        ),
                    ],
                  ),
            ),
          ),
        ),
      ),
    );
  }

  // ستاتىستىكا كارتىلىرى
  Widget _buildStatisticsCards() {
    final screenWidth = MediaQuery.of(context).size.width;
    final crossAxisCount = screenWidth < 800 ? 2 : screenWidth < 1200 ? 3 : 5;

    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 24),
      child: GridView.count(
        crossAxisCount: crossAxisCount,
        crossAxisSpacing: 20,
        mainAxisSpacing: 20,
        shrinkWrap: true,
        childAspectRatio: 1.8,
        physics: const NeverScrollableScrollPhysics(),
        children: [
          _buildStatCard(
            title: 'كىرىم-چىقىم',
            value: '¥ 45,678',
            change: '+12.5%',
            color: const Color(0xFF2196F3),
            gradientColors: [const Color(0xFF2196F3), const Color(0xFF64B5F6)],
            icon: Icons.account_balance_wallet,
          ),
          _buildStatCard(
            title: 'سودا سانى',
            value: '1,234',
            change: '+8.3%',
            color: const Color(0xFFFFA726),
            gradientColors: [const Color(0xFFFFA726), const Color(0xFFFFB74D)],
            icon: Icons.shopping_cart,
          ),
          _buildStatCard(
            title: 'ئامبار مىقدارى',
            value: '5,678',
            change: '-2.1%',
            color: const Color(0xFF42A5F5),
            gradientColors: [const Color(0xFF42A5F5), const Color(0xFF90CAF9)],
            icon: Icons.inventory_2,
          ),
          _buildStatCard(
            title: 'كىرگۈزگەن مال',
            value: '789',
            change: '+5.7%',
            color: const Color(0xFFEF5350),
            gradientColors: [const Color(0xFFEF5350), const Color(0xFFE57373)],
            icon: Icons.add_shopping_cart,
          ),
          _buildStatCard(
            title: 'نىسى تەپسىلاتى',
            value: '234',
            change: '+15.2%',
            color: const Color(0xFF66BB6A),
            gradientColors: [const Color(0xFF66BB6A), const Color(0xFF81C784)],
            icon: Icons.receipt_long,
          ),
        ],
      ),
    );
  }

  // بىر ستاتىستىكا كارتى
  Widget _buildStatCard({
    required String title,
    required String value,
    required String change,
    required Color color,
    required List<Color> gradientColors,
    required IconData icon,
  }) {
    final isPositiveChange = !change.startsWith('-');
    
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: gradientColors,
        ),
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: color.withOpacity(0.3),
            blurRadius: 8,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                title,
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 15,
                  fontWeight: FontWeight.w500,
                  letterSpacing: 0.5,
                ),
              ),
              Icon(
                icon,
                color: Colors.white.withOpacity(0.8),
                size: 24,
              ),
            ],
          ),
          const SizedBox(height: 12),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Expanded(
                child: Text(
                  value,
                  style: const TextStyle(
                    color: Colors.white,
                    fontSize: 22,
                    fontWeight: FontWeight.bold,
                    letterSpacing: 0.5,
                  ),
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                  color: isPositiveChange 
                      ? Colors.white.withOpacity(0.2)
                      : Colors.red.withOpacity(0.2),
                  borderRadius: BorderRadius.circular(12),
                ),
                child: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Icon(
                      isPositiveChange ? Icons.arrow_upward : Icons.arrow_downward,
                      color: Colors.white,
                      size: 12,
                    ),
                    const SizedBox(width: 4),
                    Text(
                      change,
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 13,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  // تېز كىرىش كۇنۇپكىلىرى
  Widget _buildQuickAccessButtons() {
    // ئەكراننىڭ كەڭلىكىگە قاراپ، ھەر قاتاردىكى كۇنۇپكا سانىنى بەلگىلەش
    final screenWidth = MediaQuery.of(context).size.width;
    final crossAxisCount = screenWidth < 800
        ? 3
        : screenWidth < 1200
            ? 4
            : 8;

    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: GridView.count(
        crossAxisCount: crossAxisCount,
        crossAxisSpacing: 16,
        mainAxisSpacing: 16,
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        children: [
          _buildQuickButton(Icons.shopping_bag, 'مال كىرگۈزۈش'),
          _buildQuickButton(Icons.local_shipping, 'مال چىقىرىش'),
          _buildQuickButton(Icons.assignment_turned_in, 'مال تاپشۇرۇش'),
          _buildQuickButton(Icons.search, 'ئامبار تەكشۈرۈش'),
          _buildQuickButton(Icons.inventory_2, 'مال ئۇچۇرى'),
          _buildQuickButton(Icons.people, 'خېرىدار ئۇچۇرى'),
          _buildQuickButton(Icons.receipt_long, 'سېتىش تالونى'),
          _buildQuickButton(Icons.description, 'كىرگۈزۈش تالونى'),
        ],
      ),
    );
  }

  // بىر تېز كىرىش كۇنۇپكىسى
  Widget _buildQuickButton(IconData icon, String title) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: () {},
        borderRadius: BorderRadius.circular(8),
        child: Container(
          padding: const EdgeInsets.all(16),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                icon,
                color: Colors.blue,
                size: 28,
              ),
              const SizedBox(height: 8),
              Text(
                title,
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Colors.grey[800],
                  fontSize: 12,
                ),
                overflow: TextOverflow.ellipsis,
              ),
            ],
          ),
        ),
      ),
    );
  }

  // دىئاگرامما قىسمى
  Widget _buildCharts() {
    // ئەكراننىڭ كەڭلىكىگە قاراپ، دىئاگراممىلارنى تىك ياكى يانتۇ تىزىش
    final screenWidth = MediaQuery.of(context).size.width;
    final isWideScreen = screenWidth >= 1200;

    if (isWideScreen) {
      // كەڭ ئېكران ئۈچۈن يانتۇ تىزىش
      return Padding(
        padding: const EdgeInsets.all(16.0),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(child: _buildSimpleSalesContainer()),
            const SizedBox(width: 24),
            Expanded(child: _buildSimpleProductsContainer()),
          ],
        ),
      );
    } else {
      // تار ئېكران ئۈچۈن تىك تىزىش
      return Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildSimpleSalesContainer(),
            const SizedBox(height: 24),
            _buildSimpleProductsContainer(),
          ],
        ),
      );
    }
  }

  // ئاددىيلاشتۇرۇلغان تىجارەت دىئاگراممىسى كونتېينېرى
  Widget _buildSimpleSalesContainer() {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(24),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          const Text(
            'تىجارەت ئەھۋالى',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.w500,
            ),
          ),
          const SizedBox(height: 16),
          // مۇرەككەپ دىئاگرامما ئورنىغا ئاددىي تېكىست
          Container(
            height: 200,
            alignment: Alignment.center,
            child: const Text(
              'تىجارەت ئەھۋالى دىئاگراممىسى (ۋاقىتلىق ئۆچۈرۈلدى)',
              style: TextStyle(
                fontSize: 16,
                color: Colors.grey,
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ],
      ),
    );
  }

  // ئاددىيلاشتۇرۇلغان بازارلىق ماللار دىئاگراممىسى كونتېينېرى
  Widget _buildSimpleProductsContainer() {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(24),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          const Text(
            'بازارلىق ماللار',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.w500,
            ),
          ),
          const SizedBox(height: 16),
          // مۇرەككەپ دىئاگرامما ئورنىغا ئاددىي تېكىست
          Container(
            height: 200,
            alignment: Alignment.center,
            child: const Text(
              'بازارلىق ماللار دىئاگراممىسى (ۋاقىتلىق ئۆچۈرۈلدى)',
              style: TextStyle(
                fontSize: 16,
                color: Colors.grey,
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ],
      ),
    );
  }

  // ئامبار دوكلاتلىرى
  Widget _buildStockReports() {
    // ئەكراننىڭ كەڭلىكىگە قاراپ، دوكلاتلارنى تىك ياكى يانتۇ تىزىش
    final screenWidth = MediaQuery.of(context).size.width;
    final isWideScreen = screenWidth >= 1200;

    if (isWideScreen) {
      // كەڭ ئېكران ئۈچۈن يانتۇ تىزىش
      return Padding(
        padding: const EdgeInsets.all(16.0),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(child: _buildSimpleLowStockContainer()),
            const SizedBox(width: 24),
            Expanded(child: _buildSimpleOverstockedContainer()),
          ],
        ),
      );
    } else {
      // تار ئېكران ئۈچۈن تىك تىزىش
      return Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildSimpleLowStockContainer(),
            const SizedBox(height: 24),
            _buildSimpleOverstockedContainer(),
          ],
        ),
      );
    }
  }

  // ئاددىيلاشتۇرۇلغان ئامباردا ئاز قالغان ماللار كونتېينېرى
  Widget _buildSimpleLowStockContainer() {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          const Text(
            'ئامباردا ئاز قالغان ماللار',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.w500,
            ),
          ),
          const SizedBox(height: 16),
          Container(
            height: 150,
            alignment: Alignment.center,
            child: const Text(
              'ئامباردا ئاز قالغان ماللار تىزىملىكى (ۋاقىتلىق ئۆچۈرۈلدى)',
              style: TextStyle(
                fontSize: 16,
                color: Colors.grey,
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ],
      ),
    );
  }

  // ئاددىيلاشتۇرۇلغان ئامباردا بېسىلىپ قالغان ماللار كونتېينېرى
  Widget _buildSimpleOverstockedContainer() {
    return Container(
      width: double.infinity,
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          const Text(
            'ئامباردا بېسىلىپ قالغان ماللار',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.w500,
            ),
          ),
          const SizedBox(height: 16),
          Container(
            height: 150,
            alignment: Alignment.center,
            child: const Text(
              'ئامباردا بېسىلىپ قالغان ماللار تىزىملىكى (ۋاقىتلىق ئۆچۈرۈلدى)',
              style: TextStyle(
                fontSize: 16,
                color: Colors.grey,
              ),
              textAlign: TextAlign.center,
            ),
          ),
        ],
      ),
    );
  }

  // يېقىنقى پائالىيەتلەر
  Widget _buildRecentActivities() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'ئەڭ يېقىنقى سودا ئەھۋالى',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.w500,
          ),
        ),
        const SizedBox(height: 16),
        Container(
          padding: const EdgeInsets.all(24),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8),
            boxShadow: [
              BoxShadow(
                color: Colors.grey.withOpacity(0.1),
                blurRadius: 4,
                offset: const Offset(0, 2),
              ),
            ],
          ),
          child: Column(
            children: [
              _buildActivityItem(
                icon: Icons.shopping_cart,
                iconColor: Colors.blue,
                iconBgColor: Colors.blue.withOpacity(0.1),
                title: 'يېڭى مال كىرگۈزۈلدى',
                subtitle: 'ئەلى ئابدۇللا تەرىپىدىن',
                time: '5 مىنۇت بۇرۇن',
              ),
              const Divider(height: 32),
              _buildActivityItem(
                icon: Icons.check_circle,
                iconColor: Colors.green,
                iconBgColor: Colors.green.withOpacity(0.1),
                title: 'سودا تاماملاندى',
                subtitle: 'مەريەم ئەخمەت تەرىپىدىن',
                time: '15 مىنۇت بۇرۇن',
              ),
              const Divider(height: 32),
              _buildActivityItem(
                icon: Icons.local_shipping,
                iconColor: Colors.amber,
                iconBgColor: Colors.amber.withOpacity(0.1),
                title: 'مال يوللاندى',
                subtitle: 'ئايگۈل تۇرسۇن تەرىپىدىن',
                time: '30 مىنۇت بۇرۇن',
              ),
            ],
          ),
        ),
      ],
    );
  }

  // بىر پائالىيەت ئۇچۇرى
  Widget _buildActivityItem({
    required IconData icon,
    required Color iconColor,
    required Color iconBgColor,
    required String title,
    required String subtitle,
    required String time,
  }) {
    return Row(
      children: [
        Container(
          width: 40,
          height: 40,
          decoration: BoxDecoration(
            color: iconBgColor,
            borderRadius: BorderRadius.circular(20),
          ),
          child: Icon(
            icon,
            color: iconColor,
          ),
        ),
        const SizedBox(width: 16),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                title,
                style: const TextStyle(
                  fontWeight: FontWeight.w500,
                ),
                overflow: TextOverflow.ellipsis,
              ),
              Text(
                subtitle,
                style: TextStyle(
                  fontSize: 14,
                  color: Colors.grey[600],
                ),
                overflow: TextOverflow.ellipsis,
              ),
            ],
          ),
        ),
        const SizedBox(width: 16),
        Text(
          time,
          style: TextStyle(
            fontSize: 14,
            color: Colors.grey[600],
          ),
        ),
      ],
    );
  }

  // تىل تاللاش تۈرى
  PopupMenuEntry<String> _buildLanguageMenuItem({
    required String flag,
    required String language,
    required bool isSelected,
  }) {
    return PopupMenuItem<String>(
      value: language,
      child: Row(
        children: [
          Text(flag, style: const TextStyle(fontSize: 24)),
          const SizedBox(width: 12),
          Text(language),
          if (isSelected) ...[
            const Spacer(),
            const Icon(Icons.check_circle, color: Colors.blue, size: 20),
          ],
        ],
      ),
    );
  }

  // ئۇسلۇب تاللاش تۈرى
  PopupMenuEntry<String> _buildThemeMenuItem({
    required IconData icon,
    required String title,
    required bool isSelected,
  }) {
    return PopupMenuItem<String>(
      value: title,
      child: Row(
        children: [
          Icon(icon, color: Colors.grey[600], size: 20),
          const SizedBox(width: 12),
          Text(title),
          if (isSelected) ...[
            const Spacer(),
            const Icon(Icons.check_circle, color: Colors.blue, size: 20),
          ],
        ],
      ),
    );
  }

  // ئۇقتۇرۇش كۆزنىكىنى كۆرسىتىش
  void _showNotificationsDialog() {
    showGeneralDialog(
      context: context,
      barrierDismissible: true,
      barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
      barrierColor: Colors.black54,
      transitionDuration: const Duration(milliseconds: 300),
      pageBuilder: (context, animation1, animation2) => Container(),
      transitionBuilder: (context, animation1, animation2, child) {
        final curvedAnimation = CurvedAnimation(
          parent: animation1,
          curve: Curves.easeInOutBack,
          reverseCurve: Curves.easeInOutBack,
        );

        return ScaleTransition(
          scale: Tween<double>(begin: 0.8, end: 1.0).animate(curvedAnimation),
          child: FadeTransition(
            opacity:
                Tween<double>(begin: 0.0, end: 1.0).animate(curvedAnimation),
            child: Dialog(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(16),
              ),
              child: AnimatedContainer(
                duration: const Duration(milliseconds: 300),
                curve: Curves.easeInOut,
                width: 400,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Container(
                      padding: const EdgeInsets.all(16),
                      decoration: const BoxDecoration(
                        color: Color(0xFF2C3E67), // سەل ئۇچۇقراق رەڭ
                        borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(16),
                          topRight: Radius.circular(16),
                        ),
                      ),
                      child: const Row(
                        children: [
                          Icon(Icons.notifications, color: Colors.white),
                          SizedBox(width: 8),
                          Text(
                            'ئۇقتۇرۇشلار',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 18,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ],
                      ),
                    ),
                    ListView(
                      shrinkWrap: true,
                      padding: const EdgeInsets.all(16),
                      children: [
                        _buildNotificationItem(
                          'يېڭى مال كىرگۈزۈلدى',
                          'ThinkPad X1 Carbon دىن 10 دانە كىرگۈزۈلدى',
                          '5 مىنۇت بۇرۇن',
                          Colors.blue,
                        ),
                        const Divider(),
                        _buildNotificationItem(
                          'مال تۈگەپ قالدى',
                          'iPhone 14 Pro نىڭ ئامباردىكى سانى 3 دانە قالدى',
                          '15 مىنۇت بۇرۇن',
                          Colors.red,
                        ),
                        const Divider(),
                        _buildNotificationItem(
                          'يېڭى سودا',
                          'HP LaserJet Pro دىن 2 دانە سېتىلدى',
                          '30 مىنۇت بۇرۇن',
                          Colors.green,
                        ),
                      ],
                    ),
                    Padding(
                      padding: const EdgeInsets.all(16),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: [
                          TextButton(
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                            child: const Text('تاقاش'),
                          ),
                          const SizedBox(width: 8),
                          ElevatedButton(
                            onPressed: () {
                              // ھەممىنى ئوقۇلدى قىلىش
                              Navigator.of(context).pop();
                            },
                            child: const Text('ھەممىنى ئوقۇلدى'),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }

  // ئۇقتۇرۇش تۈرى
  Widget _buildNotificationItem(
    String title,
    String message,
    String time,
    Color color,
  ) {
    return ListTile(
      contentPadding: EdgeInsets.zero,
      leading: Container(
        width: 40,
        height: 40,
        decoration: BoxDecoration(
          color: color.withOpacity(0.1),
          shape: BoxShape.circle,
        ),
        child: Icon(Icons.notifications, color: color),
      ),
      title: Text(
        title,
        style: const TextStyle(
          fontWeight: FontWeight.bold,
        ),
      ),
      subtitle: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const SizedBox(height: 4),
          Text(message),
          const SizedBox(height: 4),
          Text(
            time,
            style: TextStyle(
              color: Colors.grey[600],
              fontSize: 12,
            ),
          ),
        ],
      ),
    );
  }
}



